using System;
using System.Linq;

namespace IMSAT.OFTD.DOM
{
    public class CustomNodeCollection<T>:NodeCollection<T> where T:Node
    {
        private readonly Func<T, bool> checkDelegate = null;

        public CustomNodeCollection(Func<T, bool> checkDelegate, NodeCollection<Node> nodeCollection)
        {
            this.checkDelegate = checkDelegate;
            Resest(nodeCollection);
            nodeCollection.CollectionChanged += (sender, e) =>
                                                    {
                                                        if(sender is NodeCollection<Node>)
                                                        {
                                                            Resest(sender as NodeCollection<Node>);
                                                        }
                                                    };

        }

        void Resest(NodeCollection<Node> nodeCollection)
        {
            if(checkDelegate!=null)
            {
                Add(nodeCollection.Select<Node,T>(e=>(e is T)&&checkDelegate((T)e)?e as T:null).Where(e=>e!=null));
            }
            else
            {
                Add(nodeCollection.Select<Node, T>(e => (e is T) ? e as T : null).Where(e => e != null));
            }
        }
    }
}